﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Common;
using DAL;
using System.Data;

namespace Web.Admin
{
    public partial class QuanlySanpham : System.Web.UI.Page
    {
        public static bool insert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadDSSP();
            }
        }

        public void loadDSSP()
        {
            GridDSSP.DataSource = SanphamBLL.Sanpham_GetByAll();
            GridDSSP.DataBind();
            panelChitietsp.Visible = false;
            panelDSSP.Visible = true;
        }

        public void loadDSPL()
        {
            lPhanloai.Items.Clear();
            DataTable dt = PhanloaiBLL.Phanloai_GetByAll();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lPhanloai.Items.Add(dt.Rows[i]["Ten_Phan_loai"].ToString());
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            panelDSSP.Visible = false;
            panelChitietsp.Visible = true;
            txtMaSP.Enabled = true;
            ControlClass.ResetControlValues(panelChitietsp);
            lbLoi.Text = "";
            lbTenanh.Text = "";
            txtNgay.Text = Common.DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy");
            insert = true;
            loadDSPL();
        }

        protected void GridDSSP_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                insert = false;
                DataTable dt = SanphamBLL.Sanpham_GetById(id);
                panelChitietsp.Visible = true;
                panelDSSP.Visible = false;
                txtMaSP.Enabled = false;
                loadDSPL();
                lbLoi.Text = "";
                txtMaSP.Text = dt.Rows[0]["Ma_San_pham"].ToString();
                txtTenSP.Text = dt.Rows[0]["Ten_San_pham"].ToString();
                txtGia.Text = dt.Rows[0]["Gia_ban"].ToString();
                txtChitiet.Text = dt.Rows[0]["Chi_tiet"].ToString();
                lPhanloai.SelectedValue = PhanloaiBLL.Phanloai_GetById(dt.Rows[0]["Ma_Phan_loai"].ToString()).Rows[0]["Ten_Phan_loai"].ToString();
                imgHinh.ImageUrl = "../images/sanpham/"+ dt.Rows[0]["Hinh_Minh_hoa"].ToString();
                lbTenanh.Text = dt.Rows[0]["Hinh_Minh_hoa"].ToString();
                txtNgay.Text=Common.DateTimeClass.ConvertDate(dt.Rows[0]["Ngay"].ToString());
            }
            else
            {
                SanphamBLL.Sanpham_Delete(id);
                loadDSSP();
            }
        }

        protected void imgLuu_Click(object sender, ImageClickEventArgs e)
        {
            #region[Kiemtra]
            if (txtMaSP.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Mã sản phẩm");
                txtMaSP.Focus();
                return;
            }
            if (txtTenSP.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Tên sản phẩm");
                txtTenSP.Focus();
                return;
            }
            if (txtGia.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Giá bán");
                txtGia.Focus();
                return;
            }
            if (lPhanloai.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa chọn Phân loại cho sản phẩm");
                return;
            }
            #endregion
            var obj = new SanphamInfo();
            obj.Ma_San_pham = txtMaSP.Text;
            obj.Ten_San_pham = txtTenSP.Text;
            obj.Gia_ban = int.Parse(txtGia.Text);
            obj.Chi_tiet = txtChitiet.Text;
            string tenloai = lPhanloai.SelectedValue.ToString();
            obj.Ma_Phan_loai = PhanloaiBLL.Phanloai_GetByTop("", " Ten_Phan_loai = '" + tenloai+"'", "").Rows[0]["Ma_Phan_loai"].ToString();
            obj.Hinh_Minh_hoa = lbTenanh.Text;
            obj.Ngay = Common.DateTimeClass.ConvertDate(txtNgay.Text);

            if (insert)
            {
                SanphamBLL.Sanpham_Insert(obj);
            }
            else
            {
                SanphamBLL.Sanpham_Update(obj);
            }
            panelChitietsp.Visible = false;
            panelDSSP.Visible = true;
            loadDSSP();
        }

        protected void imgBack_Click(object sender, ImageClickEventArgs e)
        {
            panelChitietsp.Visible = false;
            panelDSSP.Visible = true;
        }

        protected void GridDSSP_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            GridDSSP.CurrentPageIndex = e.NewPageIndex;
            loadDSSP();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string tenanh = "";
            if (fileImg.Value.Trim().Length > 0 && fileImg.PostedFile != null && fileImg.PostedFile.ContentLength > 0)
            {
                tenanh = System.IO.Path.GetFileName(fileImg.PostedFile.FileName);
                tenanh = StringClass.Checkpath(tenanh);
                if (tenanh.Length == 0)
                {
                    lbLoi.Text = "Ảnh không phù hợp";
                    return;
                }
                else
                {
                    fileImg.PostedFile.SaveAs(Server.MapPath("/images/sanpham/") + tenanh.ToString().Trim());
                    imgHinh.ImageUrl = "../images/sanpham/" + tenanh.ToString().Trim();
                    lbTenanh.Text = tenanh;
                }
            }
        }

        protected void btnNgay_Click(object sender, ImageClickEventArgs e)
        {
            
        }
    }
}